package com.centaline.androidsalesblog.rx.database;

import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.model.databaseobject.HistoryContactDo;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;

import org.litepal.crud.DataSupport;

import rx.Observable;
import rx.Subscriber;

/**
 * 保存联系人
 * Created by guilin on 16/3/14.
 */
final class HistoryStaffsSaveOnSubscribe implements Observable.OnSubscribe<Void> {

    private final StaffDo staffDo;

    public HistoryStaffsSaveOnSubscribe(StaffDo staffDo) {
        this.staffDo = staffDo;
    }

    @Override
    public void call(Subscriber<? super Void> subscriber) {
        DataSupport.deleteAll(StaffDo.class, "cityCode = ? and staffNo = ?", AppConstant.CityCode, staffDo.getStaffNo());
        DataSupport.deleteAll(HistoryContactDo.class, "cityCode = ? and staffNo = ?", AppConstant.CityCode, staffDo.getStaffNo());
        staffDo.setCityCode(AppConstant.CityCode);
        staffDo.save();
        HistoryContactDo historyContactDo = new HistoryContactDo();
        historyContactDo.setCityCode(AppConstant.CityCode);
        historyContactDo.setStaffNo(staffDo.getStaffNo());
        historyContactDo.setDate(System.currentTimeMillis());
        historyContactDo.save();
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(null);
        }
        subscriber.onCompleted();
    }
}
